﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.PropertyManage.Controllers
{
    public class CS_MaterialSettingController : MvcControllerBase
    {

        private MaterialRuleBLL bll = new MaterialRuleBLL();
        #region View

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult MSettingForm()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult AddMaterial()
        {
            return View();
        }
        #endregion

        #region 查询数据
        /// <summary>
        /// 获取装修材料规则list
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMaterialJson(string Type)
        {
            var data = bll.GetList().ToList().Where(t => t.Type ==Type);
            data = data.OrderBy(t => t.Sort).ToList();
            return Content(data.ToJson());
        }
        /// <summary>
        /// 获取装修材料规则对象
        /// </summary>
        /// <param name="KeyValue"></param>
        /// <returns></returns>
        public ActionResult GetFormJson(string KeyValue)
        {
            MaterialRuleEntity e = bll.GetEntity(KeyValue);
            return Content(e.ToJson());
        }


        /// <summary>
        /// 获取装修材料
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult GetFileList(string keyValue)
        {
            var materiallist = new MaterialRuleBLL().GetList();
            if (!string.IsNullOrEmpty(keyValue))
            {
                var list = new MaterialSeedBLL().GetList().Where(t => t.BillCode == keyValue).ToList();
                if (list != null && list.Count > 0)
                {
                    var data = from a in materiallist
                               join b in list on a.Code equals b.RuleCode into temp
                               from tt in temp.DefaultIfEmpty()
                               select new
                               {
                                   a.Name,
                                   a.IsSubmit,
                                   a.IsUpload,
                                   RuleCode = a.Code,
                                   CommitState = tt == null ? 0 : tt.CreateDate != null ? 1 : 0,
                                   UploadState = tt == null ? 0 : !string.IsNullOrEmpty(tt.FileUrl) ? 1 : 0,
                                   Code = tt == null ? "" : tt.Code,
                                   FileName = tt == null ? "" : tt.FileName,
                                   FileUrl = tt == null ? "" : tt.FileUrl,
                                   IsQualified = tt == null ? 0 : tt.IsQualified,
                                   CreateDate = tt == null ? null : tt.CreateDate,
                                   CreateUserName = tt == null ? "" : tt.CreateUserName,
                                   ReceiveDate = tt == null ? null : tt.ReceiveDate,
                                   ReceiveUserName = tt == null ? "" : tt.ReceiveUserName,
                               };
                    return Content(data.ToJson());
                }
                else
                {
                    var data = from a in materiallist
                               select new
                               {
                                   a.Name,
                                   a.IsSubmit,
                                   a.IsUpload,
                                   RuleCode = a.Code,
                                   CommitState = 0,
                                   UploadState = 0,
                                   Code = "",
                                   FileName = "",
                                   FileUrl = "",
                                   IsQualified = 0,
                                   CreateUserName = "",
                                   ReceiveUserName = ""
                               };
                    return Content(data.ToJson());
                }
            }
            else
            {
                var data = from a in materiallist
                           select new
                           {
                               a.Name,
                               a.IsSubmit,
                               a.IsUpload,
                               RuleCode = a.Code,
                               CommitState = 0,
                               UploadState = 0,
                               Code = "",
                               FileName = "",
                               FileUrl = "",
                               IsQualified = 0,
                               CreateUserName = "",
                               ReceiveUserName = ""
                           };
                return Content(data.ToJson());
            }
        }
        #endregion

        #region 提交数据
      
        /// <summary>
        /// 上传附件
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult UploadFile(string keyValue)
        {
            try
            {
                HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
                string URL = Request.Url.ToString().Replace(Request.RawUrl, "/");
                string imgPath = "/Resource/DecorateBillFile_" + OperatorProvider.Provider.Current().CustomerCode + "/" + DateTime.Now.ToString("yyyyMMdd") + "/";
                string FileUrl = "";
                if (hfc != null && hfc.Count > 0)
                {
                    for (int i = 0; i < hfc.Count; i++)
                    {
                        if (hfc[i].FileName == "") continue;
                        else
                        {
                            string FilePath = Server.MapPath(imgPath);
                            if (!Directory.Exists(FilePath))
                            {
                                Directory.CreateDirectory(FilePath);
                            }
                            hfc[i].SaveAs(FilePath + hfc[i].FileName);
                            FileUrl = URL != "" ? FilePath.Replace(HttpRuntime.AppDomainAppPath, URL) + hfc[i].FileName : FilePath + hfc[i].FileName;
                        }
                    }
                }
                return Success("操作成功。", keyValue + "," + FileUrl);
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }
        }

        /// <summary>
        /// 保存装修材料规则
        /// </summary>
        /// <param name="m"></param>
        /// <param name="KeyValue"></param>
        /// <returns></returns>
        public ActionResult SaveMaterial(MaterialRuleEntity m, string KeyValue)
        {
            if (m != null)
            {
                m.CreateDate = System.DateTime.Now;
                if (!string.IsNullOrEmpty(m.DemoImage))
                {
                    string filePath = Server.MapPath("~/Uploads/");
                    filePath += "//" + m.DemoImage;
                    FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    int filelength;
                    filelength = (int)stream.Length; //获得文件长度 
                    //   long l = fileData.InputStream.Length; 
                    Byte[] image = new Byte[filelength]; //建立一个字节数组 
                    stream.Read(image, 0, filelength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度
                }
                m.DemoImage = null;
                bll.SaveForm(KeyValue, m);
            }

            return null;
        }

        /// <summary>
        /// 删除具体装修材料规则
        /// </summary>
        /// <param name="KeyValue"></param>
        public void RemoveForm(string KeyValue)
        {
            bll.RemoveForm(KeyValue);
        }
        #endregion
    }
}
